﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace Stock.Core
{
    public static class ObservableCollectionExtensions
    {
        public static void Sort<T>(this ObservableCollection<T> collection)
        {
            Sort(collection, Comparer<T>.Default);
        }

        public static void Sort<T>(this ObservableCollection<T> collection, IComparer<T> comparer)
        {
            if (collection == null || collection.Count < 1) return;

            var list = collection.ToList();
            list.Sort(comparer);

            var count = list.Count;

            for (int i = 0; i < count; i++)
            {
                int index = collection.IndexOf(list[i]);
                if (index == i) continue;

                collection.Move(index, i);
            }
        }
    }
}
